//导入axios
import axios from "axios";
//配置基准路径
axios.defaults.baseURL = "http://klxin.cn:8888/api/private/v1";

// 添加请求拦截器
axios.interceptors.request.use(
  function(config) {
    // 在发送请求之前做些什么
    // console.log(config, 44444);
    //获取本地存储的token
    var token = window.sessionStorage.getItem("token");
    // console.log(token,445566);
    config.headers["Authorization"] = token;
    return config;
  },
  function(error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 添加响应拦截器
axios.interceptors.response.use(
  function(response) {
    // console.log(response, 111111111);
    // 对响应数据做点什么
    return response;
  },
  function(error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

//登录请求的函数
export const login_api = obj => {
  return axios.post("/Login", obj);
};
//左侧菜单栏请求的函数
export const menus_api = () => {
  return axios.get("/menus");
};
//用户数据列表
export const users_api = params => {
  return axios.get("/users", {
    params
  });
};

//修改用户状态  users/:uId/state/:type
export const usersState_api = obj => {
  return axios.put(`users/${obj.uId}/state/${obj.type}`);
};

//删除单个用户  users/:id
export const usersDel_api = id => {
  return axios.delete(`users/${id}`);
};

//编辑用户提交   users/:id
export const usersEdit_api = obj => {
  return axios.put(`users/${obj.id}`, { email: obj.email, mobile: obj.mobile });
};

//添加用户
export const usersAdd_api = obj => {
  return axios.post("/users", obj);
};

// 所有权限列表  rights/:type
export const rights_api = type => {
  return axios.get(`/rights/${type}`);
};

//角色列表   roles
export const roles_api = () => {
  return axios.get(`/roles`);
};

//9-删除用户角色的权限   roles/:roleId/rights/:rightId
export const rightsDel_api = obj => {
  return axios.delete(`/roles/${obj.roleId}/rights/${obj.rightId}`);
};

//角色授权 roles/:roleId/rights
export const addRoleRights_api = obj => {
  return axios.post(`/roles/${obj.roleId}/rights`, { rids: obj.rids });
};

//分配用户角色   users/:id/role
export const addUserRole_api = obj => {
  return axios.put(`users/${obj.id}`, { rid: obj.rid });
};

//获取  商品列表  goods

export const goods_api = params => {
  return axios.get("/goods", {
    params
  });
};

//删除商品

//商品分类数据列表  categories
export const cate_api = obj => {
  return axios.get(`categories`, {
    params: {
      type: obj.type,
      pagenum: obj.pagenum,
      pagesize: obj.pagesize
    }
  });
};

//  参数列表    categories/:id/attributes
export const params_api = obj => {
  return axios.get(`categories/${obj.id}/attributes`, {
    params: {
      sel: obj.sel
    }
  });
};

//添加动态参数或者静态属性  categories/:id/attributes
export const addParams_api = obj => {
  return axios.post(`categories/${obj.id}/attributes`, {});
};

//编辑提交参数   categories/:id/attributes/:attrId
export const addParamsput_api = obj => {
  return axios.put(`categories/${obj.id}/attributes/${obj.attrId}`, {
    attr_name: obj.attr_name,
    attr_sel: obj.attr_sel,
    attr_vals: obj.attr_vals
  });
};
